package com.dashu.authority.servie.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dashu.authority.common.Constant;
import com.dashu.authority.common.MessageCode;
import com.dashu.authority.entity.Application;
import com.dashu.authority.exception.AuthorityException;
import com.dashu.authority.repository.ApplicationMapper;
import com.dashu.authority.servie.ApplicationService;
import com.dashu.authority.util.AuthorityServiceUtil;
import com.github.pagehelper.PageHelper;


@Service
public class ApplicationServiceImpl extends BaseServiceImpl<Application> implements ApplicationService {
	
	@Autowired
	private ApplicationMapper applicationMapper;

	@Override
	public List<Application> queryAllByExample(Application entity,int pageNum, int pageSize) throws AuthorityException{
		PageHelper.startPage(pageNum, pageSize);
		Map<Object, Object> params = new HashMap<Object, Object>();
		if (entity!=null) {
			if (entity.getApplicationName()!=null && entity.getApplicationName()!="") {
				params.put("applicationName", entity.getApplicationName());
			}
		}
		params.put("orderBy", "create_time");
		params.put("sort", "desc");
		return applicationMapper.queryAllByExample(params);
	}

	@Override
	public Map<String, Object> insertEntity(Application entity) throws AuthorityException{
		int ret = applicationMapper.insertEntity(entity);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		}else{
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
	}

	@Override
	public Map<String, Object> updateEntity(Application entity) throws AuthorityException {
		int ret = applicationMapper.updateEntity(entity);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_SAVE);
		}else{
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_SAVE);
		}
		
	}

	@Override
	public Map<String, Object> deleteByIds(Integer[] ids) throws AuthorityException {
		if(ids==null || ids.length==0){
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(),"信息id数组:");
		}
		Map<Object,Object> params=new HashMap<Object,Object>();
		params.put("ids", ids);
		int ret=applicationMapper.deleteByIds(params);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		}else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}

	@Override
	public Map<String, Object> updateStatus(Integer[] ids,String status) throws AuthorityException {
		if(ids==null || ids.length==0){
			throw new AuthorityException(MessageCode.ERROR_PARAMS_NULL.toString(),"信息id数组:");
		}
		Map<Object,Object> params=new HashMap<Object,Object>();
		params.put("ids", ids);
		params.put("status", status);
		int ret=applicationMapper.updateStatus(params);
		if(ret>0){
			return AuthorityServiceUtil.OperaSuccessMap(Constant.OPERATION_DELETE);
		}else {
			return AuthorityServiceUtil.OperaFailMap(Constant.OPERATION_DELETE);
		}
	}
	
}
